/**********************************************************
 * File
 *	database.js
 * Description
 *	Handles creation and interaction with the badpass
 *	common passwords database.
 *********************************************************/

/*
 * Name: initDatabase()
 * Desc: Creates the default common passwords database if it doesn't already
 *		 exist.
 */
badpass.initDatabase = function() {
	//Test whether the database exists before creating the default database
	if(badpass.databaseFileExists() == true) {
		var conn = badpass.getDatabaseConnection();
		if(conn.tableExists("common_passwords") == true) {
			//The database exists, return
			conn.close();
			return;
		}
		conn.close();
	}
	
	//Create the default database
	badpass.installDefaultDatabase();
}

/*
 * Name: installDefaultDatabase()
 * Desc: Copies the default database to the user profile directory.
 */
badpass.installDefaultDatabase = function() {
	//Get an nsIFile representing the default database file
	var defaultdb = Components.classes["@mozilla.org/file/directory_service;1"]
									.getService(Components.interfaces.nsIProperties)
									.get("ProfD", Components.interfaces.nsIFile);
	defaultdb.append("extensions");
	defaultdb.append("badpass@nickbloor.co.uk");
	defaultdb.append("defaults");
	defaultdb.append("badpass.sqlite");
	
	//Get an nsIFile representing the [user profile]/badpass directory
	var targetdir = Components.classes["@mozilla.org/file/directory_service;1"]
									.getService(Components.interfaces.nsIProperties)
									.get("ProfD", Components.interfaces.nsIFile);
	targetdir.append("badpass");
	if(targetdir.exists() == false) {
		targetdir.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755);
	}
	
	//Copy the default database to the user profile
	defaultdb.copyTo(targetdir, null);
}

/*
 * Name: databaseFileExists()
 * Desc: Returns true if the database file exists in the user profile
 * 		 directory.
 */
badpass.databaseFileExists = function() {
	//Check if the database file exists using nsIFile
	var dbfile = Components.classes["@mozilla.org/file/directory_service;1"]
									.getService(Components.interfaces.nsIProperties)
									.get("ProfD", Components.interfaces.nsIFile);
	dbfile.append("badpass");
	dbfile.append("badpass.sqlite");
	return dbfile.exists();
}

/*
 * Name: getDatabaseConnection()
 * Desc: Creates and returns a connection to the BadPass
 *		 database file.
 */
badpass.getDatabaseConnection = function() {
	//Get an nsIFile object pointing at the common passwords database file in
	//[profiledata]/badpass/badpass.sqlite, creating the badpass directory if
	//required.
	var dbfile = Components.classes["@mozilla.org/file/directory_service;1"]
									.getService(Components.interfaces.nsIProperties)
									.get("ProfD", Components.interfaces.nsIFile);
	dbfile.append("badpass");
	dbfile.append("badpass.sqlite");
	
	//Get the storage service
	var storageService = Components.classes["@mozilla.org/storage/service;1"]
									.getService(Components.interfaces.mozIStorageService);
	
	//Open and return a database connection
	return storageService.openDatabase(dbfile);
}
